<?php 
/*
   Copyright 2010, Sanket Parab

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
   
   This is part of the e-TAT Architecture Package and will be featured in the final e-TAT Application (Phase 1).
   
   Holds data on all courses created by the instructor.
 */
class Course {
	/**
	 * Constructor
	 * @param $id ID of the Course
	 */
	function __construct($id) {
		$query = mysql_query("SELECT * FROM `course` WHERE `id` = '$id';") or die(mysql_error());
		$result = mysql_fetch_object($query);
		if($result) {
			foreach($result as $key=>$value) {
				$this->$key = $value;
			}
		}
	}
	/**
	 * Update course details.
	 */
	public function updateCourse() {
		$query = mysql_query("UPDATE `course` SET `owner_id` = '$this->owner_id', `title` = '$this->title', `term` = '$this->term', `description` = '$this->description' WHERE `id` = '$this->id'") or die(mysql_error());
		return true;
	}
	/**
	 * Returns an array containing Instructor instances corresponding to each owner of the course.
	 */
	public function getAllOwners() {
		if(strlen($this->owner_id) == 1) { //If there is only one owner.
			return new Instructor($this->owner_id); 
		} else { // Multiple owners
			$all = explode(',',$this->owner_id);
			foreach($all as $a) {
				$owners[] = new Instructor($a);
			}
			return $owners;
		}
	}
	/**
	 * Returns an array of Group instances present in the course.
	 */
	public function getAllGroups() { 
		$query = mysql_query("SELECT * FROM `group` WHERE `course_id` = '$this->id'") or die(mysql_error());
		while($r = mysql_fetch_object($query)) {
			$groups[] = new Group($r->id);
		}
		return $groups;
	}
	/**
	 * Returns a single Group instance corresponding to the $id (Group ID) param.
	 */
	public function getGroup($id) {
		$query = mysql_query("SELECT * FROM `group` WHERE `id` = '$id'") or die(mysql_error());
		$r = mysql_fetch_object($query);
		if($r) {
			$group = new Group($r->id);
			return $group;
		} else {
			return false;
		}
	}
	/**
	 * Returns an array of Student instances present in the course.
	 */
	public function getAllStudents() {
		$groups = $this->getAllGroups();
		if($groups) { //Students can be fetched from a Group. So fetch all groups first.
			foreach($groups as $g) {
				$students = $g->getAllStudents();
				if($students) {
					foreach($students as $s) {
						$s->group_name = $g->name;
						$all[] = $s;
					}
				}
			}
		}
		if($all) {
			return $all;
		} else {
			return false;
		}
	}
	/**
	 * Adds another owner to the course.
	 * @param $owner_id requires the owner id
	 */
	public function addOwner($owner_id) {
		if(strlen($this->owner_id) == 1) { // Single owner.
			if($this->owner_id == $owner_id) {
				return false;
			} else {
				$this->owner_id .= ','.$owner_id; // Attached the second owner.
			}
		} else { // Multiple owners
			$all = explode(',',$this->owner_id);
			foreach($all as $key=>$value) {
				if($owner_id == $value)
				break;
			}
			$all[] = $owner_id;
			$this->owner_id = implode(',',$all);
		}
		$this->updateCourse();
	}
	/**
	 * Adds another Group to the course.
	 * @param $data OBJECT ARRAY
	 * $data->name Group Name,
	 * $data->description Group Description
	 * $data->code_name Google Code URL
	 * $data->groups_name Google Groups URL
	 */
	public function addGroup($data) {
		$query = mysql_query("INSERT INTO `group` VALUES (NULL,'$data->name','$data->description',NULL,'$this->id','$data->code_name','$data->groups_name',0,0,0,0,0);") or die(mysql_error());
		return true;
	}
	/**
	 * Remove a Group from the course.
	 * @param $id requires the group id
	 */
	public function removeGroup($id) {
		$group = $this->getGroup($id);
		if($group) {
			$group->remove();
			return true;
		} else {
			return false;
		}
	}
	public function getAnnotations() {
		$query = mysql_query("SELECT * FROM `annotation` WHERE `instructor_id` = '$this->owner_id' AND `course_id` = '$this->id'");
		while($r = mysql_fetch_object($query)) {
			$annotations[] = new Annotation($r->id);
		}
		return $annotations;
	}
}